package com.sy.rabbitmq.qos;

import com.rabbitmq.client.*;
import com.sy.rabbitmq.TestProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消息消费限流 -生产端
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建ConnectionFactory
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(TestProperties.getIp());
        factory.setPort(TestProperties.getPort());
        factory.setVirtualHost("/");

        //2.获取Connection
        Connection connection = factory.newConnection();

        //3.获取channel
        Channel channel = connection.createChannel();

        //4.指定消息的投递模式: confirmSelect: 消息的确认模式
        channel.confirmSelect();

        String exchangeName = "test_qos_exchange";
        String routeKey = "qos.routeKey";

        //5.发送一条消息
        String msg = "Hello RabbitMQ send qos message!";
        int i = 0;
        while(i<5){
            channel.basicPublish(exchangeName,routeKey,true,null,msg.getBytes());
            i++;
            System.out.println(i);
        }
        channel.close();
    }
}
